'use strict';

const Service = require('egg').Service;
const Op = require('sequelize').Op;

class OrderService extends Service {
  async addCar(shopInfo) {
    const {
      ctx,
    } = this;
    const selOrder = await ctx.model.TbOrder.findOne({
      where: {
        pid: shopInfo.pid,
        [Op.and]: {
          car: 1,
        },
      },
    });
    if (selOrder) {
      const num = shopInfo.num + selOrder.dataValues.num;
      const result = await ctx.model.TbOrder.update({
        num,
      }, {
        where: {
          id: selOrder.dataValues.id,
        },
      });
      if (result) {
        if (result) {
          return {
            status: 200,
            data: {
              msg: '加入购物车成功',
            },
          };
        }
        return {
          status: 400,
          data: {
            msg: '加入购物车失败，请重试',
          },
        };
      }
    }
    const result = await ctx.model.TbOrder.create({
      uid: shopInfo.userid,
      pid: shopInfo.pid,
      car: shopInfo.car,
      orderimg: shopInfo.orderimg,
      title: shopInfo.title,
      price: shopInfo.price,
      num: shopInfo.num,
      freight: shopInfo.freight,
    });
    if (result) {
      return {
        status: 200,
        data: {
          msg: '加入购物车成功',
        },
      };
    }
    return {
      status: 400,
      data: {
        msg: '加入购物车失败，请重试',
      },
    };
  }
  async setOrder(orderInfo) {
    const {
      ctx,
    } = this;
    const result = [];
    for (const item of orderInfo) {
      const res = await ctx.model.TbOrder.update({
        address: item.address,
        consignee: item.consignee,
        tel: item.tel,
        createdDate: item.createdDate,
        distribution: item.distribution,
        invoiceinfo: item.invoiceinfo,
        ordernumber: item.ordernumber,
        totalprice: item.totalprice,
        state: item.state,
        car: item.car,
        carcheck: item.carcheck,
      }, {
        where: {
          id: item.id,
        },
      });
      result.push(res);
    }
    if (result) {
      await this.app.redis.setex(orderInfo.id + 'o', 60 * 30, orderInfo.state);
      return {
        status: 200,
      };
    }
    return {
      status: 400,
    };
  }
  async getOrders(userid, pagesize, page) {
    const {
      ctx,
    } = this;
    const result = await ctx.model.TbOrder.findAll({
      where: {
        uid: userid,
        [Op.and]: {
          ordernumber: {
            [Op.not]: null,
          },
        },
      },
      offset: (page - 1) * pagesize, // 每页起始位置
      limit: parseInt(pagesize), // 每页的数据条数
      order: [
        [ 'createdDate', 'DESC' ],
      ],
    });
    if (result) {
      return {
        status: 200,
        data: {
          result,
        },
      };
    }
    return {
      status: 400,
    };
  }
  async getStateOrders(userid, pagesize, page, state) {
    const {
      ctx,
    } = this;
    const result = await ctx.model.TbOrder.findAll({
      where: {
        uid: userid,
        [Op.and]: {
          ordernumber: {
            [Op.not]: null,
          },
          state,
        },
      },
      offset: (page - 1) * pagesize, // 每页起始位置
      limit: parseInt(pagesize), // 每页的数据条数
      order: [
        [ 'createdDate', 'DESC' ],
      ],
    });
    if (result) {
      return {
        status: 200,
        data: {
          result,
        },
      };
    }
    return {
      status: 400,
    };
  }
}
module.exports = OrderService;
